import {login, mobileLogin} from '@/api/user'
// import { jsonInBlacklist } from '@/api/jwt'
import router from '@/router/index'

export const user = {
    namespaced: true,
    state: {
        userInfo: {
            uuid: "",
            nickName: "",
            headImgUrl: "",
            authority: "",
            roleId: "",
            areaId: "",
            areaName: ""
        },
        token: "",
        expiresAt: ""
    },
    mutations: {
        setUserInfo(state, userInfo) {
            // 这里的 `state` 对象是模块的局部状态
            state.userInfo = userInfo
        },
        setToken(state, token) {
            // 这里的 `state` 对象是模块的局部状态
            state.token = token
        },
        setExpiresAt(state, expiresAt) {
            // 这里的 `state` 对象是模块的局部状态
            state.expiresAt = expiresAt
        },
        LoginOut(state) {
            state.userInfo = {}
            state.token = ""
            state.expiresAt = ""
            router.push({name: 'login', replace: true})
            sessionStorage.clear()
            window.location.reload()
        },
        ResetUserInfo(state, userInfo = {}) {
            state.userInfo = {
                ...state.userInfo,
                ...userInfo
            }
        }
    },
    actions: {
        async LoginIn({commit}, loginInfo) {
            const res = await login(loginInfo)
            commit('setToken', res.headers.authorization)
            let headImgUrl = res.data.data.headImgUrl ? res.data.data.headImgUrl : 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg'
            let userInfo = {
                headImgUrl: headImgUrl,
                nickName: res.data.data.userName,
                roleId: res.data.data.roleId,
                areaId: res.data.data.areaId,
                areaName: res.data.data.areaName
            }
            commit('setUserInfo', userInfo)
            if (res.data.statusCode === 0) {
                if (res.data.data.roleId === 3 && res.data.data.auditStatus !== 3) {
                    router.push({path: '/layout/organization'})
                } else {
                    router.push({path: '/layout/dashboard'})
                }

            }
        },
        async MobileLoginIn({commit}, loginInfo) {
            const res = await mobileLogin(loginInfo)
            commit('setToken', res.headers.authorization)
            let headImgUrl = res.data.data.headImgUrl ? res.data.data.headImgUrl : 'https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg'
            let userInfo = {
                headImgUrl: headImgUrl,
                nickName: res.data.data.userName,
                roleId: res.data.data.roleId,
                areaId: res.data.data.areaId,
                areaName: res.data.data.areaName
            }
            commit('setUserInfo', userInfo)
            if (res.data.statusCode === 0) {
                if (res.data.data.roleId === 3 && res.data.data.auditStatus !== 3) {
                    router.push({path: '/layout/organization'})
                } else {
                    router.push({path: '/layout/dashboard'})
                }

            }
        },
        async LoginOut({commit}) {
            commit("LoginOut")
        }
    },
    getters: {
        userInfo(state) {
            return state.userInfo
        },
        token(state) {
            return state.token
        },
        expiresAt(state) {
            return state.expiresAt
        }
    }
}